﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Catering_DBOpertion;
using System.Data;

namespace Catering_Operation
{
    public class AreaRules
    {
        #region  Method

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from AreaRules");
            strSql.Append(" where ARID=@ARID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ARID", SqlDbType.Int,4)};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Catering_Model.AreaRules model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into AreaRules(");
            strSql.Append("AreaID,RawIDs,ARName)");
            strSql.Append(" values (");
            strSql.Append("@AreaID,@RawIDs,@ARName);select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@AreaID", SqlDbType.Int,4),
					new SqlParameter("@RawIDs", SqlDbType.NVarChar,4000),
					new SqlParameter("@ARName", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.AreaID;
            parameters[1].Value = model.RawIDs;
            parameters[2].Value = model.ARName;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Catering_Model.AreaRules model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update AreaRules set ");
            strSql.Append("AreaID=@AreaID,");
            strSql.Append("RawIDs=@RawIDs,");
            strSql.Append("ARName=@ARName");
            strSql.Append(" where ARID=@ARID ");
            SqlParameter[] parameters = {
					new SqlParameter("@AreaID", SqlDbType.Int,4),
					new SqlParameter("@RawIDs", SqlDbType.NVarChar,4000),
					new SqlParameter("@ARName", SqlDbType.NVarChar,50),
					new SqlParameter("@ARID", SqlDbType.Int,4)};
            parameters[0].Value = model.AreaID;
            parameters[1].Value = model.RawIDs;
            parameters[2].Value = model.ARName;
            parameters[3].Value = model.ARID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ARID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from AreaRules ");
            strSql.Append(" where ARID=@ARID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ARID", SqlDbType.Int,4)};
            parameters[0].Value = ARID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除多条数据
        /// </summary>
        public bool DeleteList(int AreaID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from AreaRules ");
            strSql.Append(" where AreaID=@AreaID ");
            SqlParameter[] parameters = { new SqlParameter("@AreaID", SqlDbType.Int, 4) };
            parameters[0].Value = AreaID;
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Catering_Model.AreaRules GetModel(int ARID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ARID,RawIDs,AreaID,ARName from AreaRules ");
            strSql.Append(" where ARID=@ARID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ARID", SqlDbType.Int,4)};
            parameters[0].Value = ARID;

            Catering_Model.AreaRules model = new Catering_Model.AreaRules();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ARID"].ToString() != "")
                {
                    model.ARID = int.Parse(ds.Tables[0].Rows[0]["ARID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["RawIDs"] != null)
                {
                    model.RawIDs = ds.Tables[0].Rows[0]["RawIDs"].ToString();
                }
                if (ds.Tables[0].Rows[0]["AreaID"].ToString() != "")
                {
                    model.AreaID = int.Parse(ds.Tables[0].Rows[0]["AreaID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ARName"] != null)
                {
                    model.ARName = ds.Tables[0].Rows[0]["ARName"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ARID,RawIDs,AreaID,ARName ");
            strSql.Append(" FROM AreaRules ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ARID,RawIDs,AreaID,ARName ");
            strSql.Append(" FROM AreaRules ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        #endregion  Method
    }
}
